﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class PlanInventory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("NA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["flt_no"] == null)
                {
                    lblError.Text = "Invalid page call!";
                    return;
                }
                if (Request.QueryString["id"] == null)
                {
                    lblError.Text = "Invalid page call!";
                    return;
                }

                lblAirline.Text = Session["AIRLINE"].ToString();
                lblFltNo.Text = Request.QueryString["flt_no"].ToString();
                lblPlanID.Text = Request.QueryString["id"].ToString();
                hidPlanFlt.Value = lblFltNo.Text;
                hidPlanID.Value = lblPlanID.Text;
                hidPlanFlt2.Value = lblFltNo.Text;
                hidPlanID2.Value = lblPlanID.Text;
                lstAirines.SelectedValue = lblAirline.Text;
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
            GridView2.DataBind();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            lblCabinError.Text = "";
            GridView1.DataBind();
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_clear_sub_tables";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@id", lblPlanID.Text);

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblCabinError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblCabinError.Text = "Failed! Check plan status.";
                else
                    lblCabinError.Text = "Unknow error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSetMaxOpn_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you want to edit.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_set_cabin";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[1]);
                para = cmd.Parameters.AddWithValue("@id", strArray[2]);
                para = cmd.Parameters.AddWithValue("@airport", strArray[3]);
                para = cmd.Parameters.AddWithValue("@cabin", strArray[4]);
                para = cmd.Parameters.AddWithValue("@max_opn", txtValue.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblCabinError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblCabinError.Text = "Plan not found or bad status!";
                else if (ret_code == 2)
                    lblCabinError.Text = "Update failed!";
                else
                    lblCabinError.Text = "Unknow error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }


        protected void SetCabinDcp(string airline, string flt_no, string id, string airport, string cabin, string cabin_dcp)
        {
            //if (Session["ACL"].ToString().IndexOf("AJ2") < 0)
            //{
            //    lblError.Text = "unauthorized";
            //    return;
            //}
            //int ret_code;
            //SqlCommand cmd = new SqlCommand();
            //lblError.Text = "";
            //try
            //{
            //    cmd.CommandText = "usp_set_plan_cabin_dcp";
            //    SqlParameter para = cmd.Parameters.AddWithValue("@airline", airline);
            //    para = cmd.Parameters.AddWithValue("@flt_no", flt_no);
            //    para = cmd.Parameters.AddWithValue("@id", id);
            //    para = cmd.Parameters.AddWithValue("@airport", airport);
            //    para = cmd.Parameters.AddWithValue("@cabin", cabin);
            //    para = cmd.Parameters.AddWithValue("@cabin_dcp", cabin_dcp);
            //    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

            //    ret_code =TinyDAL.Exec(ref cmd);
            //    if (ret_code == 0)
            //        lblError.Text = "Done.";
            //    else if (ret_code == 1)
            //        lblError.Text = "Plan not found or bad status!";
            //    else if (ret_code == 2)
            //        lblError.Text = "Update failed!";
            //    else
            //        lblError.Text = "Unknow error!";
            //}
            //catch (Exception ex)
            //{
            //    lblError.Text = ex.Message;
            //}

            //GridView1.DataBind();
        }
        protected void btnSetDcp_Click(object sender, EventArgs e)
        {
            //if (Session["ACL"].ToString().IndexOf("AJ2") < 0)
            //{
            //    lblError.Text = "unauthorized";
            //    return;
            //}
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select the record you want to edit.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');

            //SetCabinDcp(strArray[0], strArray[1], strArray[2], strArray[3], strArray[4], lstDcps.SelectedValue);
        }
        protected void btnClearDcp_Click(object sender, EventArgs e)
        {
            //if (Session["ACL"].ToString().IndexOf("AJ2") < 0)
            //{
            //    lblError.Text = "unauthorized";
            //    return;
            //}
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select the record you want to edit.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');

            //SetCabinDcp(strArray[0], strArray[1], strArray[2], strArray[3], strArray[4], "#");
        }
        protected void btnRefresh2_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            GridView2.DataBind();
        }
        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;


            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_rebuild_class_nesting";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblPlanID.Text);

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView2.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "updating total_avail failed!";
                else
                    lblError.Text = "Unknow error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the record you want to edit.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_set_class";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblPlanID.Text);
                para = cmd.Parameters.AddWithValue("@ori_airport", strArray[0]);
                para = cmd.Parameters.AddWithValue("@des_airport", strArray[1]);
                para = cmd.Parameters.AddWithValue("@class", strArray[2]);
                para = cmd.Parameters.AddWithValue("@class_sn", txtClassSn.Text);
                para = cmd.Parameters.AddWithValue("@status", lstStatus2.SelectedValue);
                para = cmd.Parameters.AddWithValue("@max_opn", txtMaxOpn2.Text);
                if (chkNested.Checked)
                    para = cmd.Parameters.AddWithValue("@nested", 1);
                else
                    para = cmd.Parameters.AddWithValue("@nested", 0);
                //para = cmd.Parameters.AddWithValue("@blk", txtBlk2.Text);
                //if (chkUseDcp2.Checked)
                //    para = cmd.Parameters.AddWithValue("@class_dcp", lstDcp2.SelectedValue);
                //else
                //    para = cmd.Parameters.AddWithValue("@class_dcp", "-");
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView2.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Plan/class not found!";
                else if (ret_code == 2)
                    lblError.Text = "Improper status!";
                else
                    lblError.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the class you want to remove.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_plan_delete_class";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblPlanID.Text);
                para = cmd.Parameters.AddWithValue("@ori_airport", strArray[0]);
                para = cmd.Parameters.AddWithValue("@des_airport", strArray[1]);
                para = cmd.Parameters.AddWithValue("@class", strArray[2]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView2.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Check plan status!";
                else if (ret_code == 2)
                    lblError.Text = "Update failed!";
                else
                    lblError.Text = "Unknow error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_plan_add_class");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblPlanID.Text);
                para = cmd.Parameters.AddWithValue("@ori_airport", lstOri.SelectedValue);
                para = cmd.Parameters.AddWithValue("@des_airport", lstDes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@class", lstClasses.SelectedValue);
                if (chkNested2.Checked)
                    para = cmd.Parameters.AddWithValue("@nested", 1);
                else
                    para = cmd.Parameters.AddWithValue("@nested", 0);
                para = cmd.Parameters.AddWithValue("@status", lstStatus.SelectedValue);
                para = cmd.Parameters.AddWithValue("@max_opn", txtMaxOpn.Text);
                para = cmd.Parameters.AddWithValue("@market_airline", lstAirines.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@blk", txtBlk.Text);
                //if (chkUseDcp.Checked)
                //    para = cmd.Parameters.AddWithValue("@class_dcp", lstDcp.SelectedValue);
                //else
                //para = cmd.Parameters.AddWithValue("@class_dcp", "-");
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView2.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Cabin not found!";
                else if (ret_code == 2)
                    lblError.Text = "Segment not found!";
                else if (ret_code == 3)
                    lblError.Text = "Insert failed!";
                else if (ret_code == 4)
                    lblError.Text = "Check plan status!";
                else
                    lblError.Text = "Unknow error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void lstCabins_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataBind();
        }
        //protected void btnDbfDetails_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("class_dbf.aspx?KEY=" + lstDcp2.SelectedValue);
        //}
        protected void btnSetDBF_Click(object sender, EventArgs e)
        {
            //if (Session["ACL"].ToString().IndexOf("AJ2") < 0)
            //{
            //    lblError.Text = "unauthorized";
            //    return;
            //}
            //int ret_code;
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //{
            //    lblError.Text = "Please select the record you want to edit.";
            //    return;
            //}
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');

            //SqlCommand cmd = new SqlCommand();
            //lblError.Text = "";
            //try
            //{
            //    cmd.CommandText = "usp_set_plan_class_dbf";
            //    SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            //    para = cmd.Parameters.AddWithValue("@flt_no", lblFltNo.Text);
            //    para = cmd.Parameters.AddWithValue("@plan_id", lblPlanID.Text);
            //    para = cmd.Parameters.AddWithValue("@ori_airport", strArray[0]);
            //    para = cmd.Parameters.AddWithValue("@des_airport", strArray[1]);
            //    para = cmd.Parameters.AddWithValue("@class", strArray[2]);
            //    if (chkUseDcp2.Checked)
            //        para = cmd.Parameters.AddWithValue("@class_dbf", lstDcp2.SelectedValue);
            //    else
            //        para = cmd.Parameters.AddWithValue("@class_dbf", "-");
            //    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());

            //    ret_code = TinyDAL.Exec(ref cmd);
            //    if (ret_code == 0)
            //    {
            //        lblError.Text = "Done.";
            //        GridView2.DataBind();
            //    }
            //    else if (ret_code == 1)
            //        lblError.Text = "Setting failed!";
            //    else if (ret_code == 2)
            //        lblError.Text = "Improper status!";
            //    else
            //        lblError.Text = "Error:" + ret_code.ToString();
            //}
            //catch (Exception ex)
            //{
            //    if (Application["DEBUG"].ToString() == "1")
            //        lblError.Text = ex.Message;
            //    else
            //        lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            //}
        }
        protected void btnProducts_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the class you wanted.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            Response.Redirect("ClassAvailProducts.aspx?flt_no=" + lblFltNo.Text + "&flt_date=" + txtFltDate.Text + "&plan_id=" + lblPlanID.Text.TrimEnd() + "&ori=" + strArray[0] + "&des=" + strArray[1] + "&class=" + strArray[2]);
        }
    }
}